package com.scada.service.impl;

import java.util.HashMap;
import java.util.Map;

import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scada.dao.ILectureInfoDao;
import com.scada.inf.LectureInfoVisitor;
import com.scada.model.bean.LectureInfo;
import com.scada.model.result.ResponseBean;
import com.scada.model.search.LectureInfoSearch;

@Service("lectureInfoVisitorService")
@WebService
@SOAPBinding(style = Style.RPC)
public class LectureInfoVisitorImpl implements LectureInfoVisitor{

	@Autowired
	private ILectureInfoDao lectureInfoDao;
	
	@Override
	@Transactional
	public void deleteLectureInfoById(String isShow, String ids) {
		
		try {
			Map<String,Object> map = null;
			for(String id : ids.split(",")) {
				map = new HashMap<String,Object>(); 
				map.put("isShow", isShow);
				map.put("id", Long.parseLong(id));
				lectureInfoDao.deleteLectureInfoById(map);
			}
		}catch (Exception e) {
			throw new RuntimeException();
		}
	}

	@Override
	public ResponseBean insertLectureInfo(LectureInfo lectureInfo) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			lectureInfoDao.insertLectureInfo(lectureInfo);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getLectureInfoById(Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			responseBean.addObject("result", lectureInfoDao.getLectureInfoById(id));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean updateLectureInfo(LectureInfo lectureInfo) {
		
		ResponseBean responseBean = new ResponseBean();
		try{
			lectureInfoDao.updateLectureInfo(lectureInfo);
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean getLectureInfoPageData(LectureInfoSearch lectureInfoSearch) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			map.put("isShow", LectureInfo.IsShowCode.YES.getKey());
			map.put("lectureNum", lectureInfoSearch.getLectureNum());
			map.put("title", lectureInfoSearch.getTitle());
			map.put("startDate", lectureInfoSearch.getStartDate());
			map.put("endDate", lectureInfoSearch.getEndDate());
			responseBean.addObject("resultCount", lectureInfoDao.getLectureInfoPageCount(map));
			map.put("start", lectureInfoSearch.getStart());
			map.put("pageSize", lectureInfoSearch.getLength());
            responseBean.addObject("resultList", lectureInfoDao.getLectureInfoPageList(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

	@Override
	public ResponseBean validateNumRepeat(String lectureNum, Long id) {
		
		ResponseBean responseBean = new ResponseBean();
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("lectureNum", lectureNum);
		map.put("id", id);
		try{
			responseBean.addObject("result", lectureInfoDao.validateNumRepeat(map));
			responseBean.success();
		}catch(Exception e){
			responseBean.failed();
		}
		return responseBean;
	}

}
